package com.vcredit.creditcat.manager.agent;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.ICustAgentManager;
import com.vcredit.creditcat.model.CustAgentVo;
import com.vcredit.creditcat.model.UserAgent;
import com.vcredit.creditcat.service.UserAgentService;
import com.vcredit.creditcat.util.EncoderHandler;

/**
 * 推荐二维码
 * 
 * @author chengxin
 *
 */
@Service(protocol = { "protocol1" }, version = "1.0")
public class CustAgentManager implements ICustAgentManager {
	@Value("${cust.agent.url}")
	private String agentUrl;
	@Autowired
	private UserAgentService userAgentService;

	/**
	 * 代理人----- 推荐二维码
	 */
	@Override
	public CustAgentVo CustAgentResource(Long custId) throws Exception {
		// TODO Auto-generated method stub
		UserAgent userAgent = new UserAgent();
		userAgent.setCustId(custId);
		// userAgent.setStatus(1);
		List<UserAgent> find = userAgentService.find(userAgent);
		Long agentId = 0L;
		String url = agentUrl;
		if (find.size() > 0) {
			agentId = find.get(0).getUserAgentId();
			// agentId
			url = agentUrl + "?agentId=" + agentId;
		} else {
			url = agentUrl;
		}
		CustAgentVo custAgentVo = new CustAgentVo();
		EncoderHandler handler = new EncoderHandler();
		int width = 400, height = 400;
		String base64 = handler.encode(url, width, height, "");
		custAgentVo.setImgBase(base64);
		custAgentVo.setUrl(url);
		return custAgentVo;
	}

}
